<?php

namespace app\models;

use Yii;
use yii\helpers\Html;
/**
 * This is the model class for table "{{%app}}".
 *
 * @property int $id
 * @property string $name 应用名称
 * @property string $own 项目开发者
 * @property string $uid 上传用户id
 * @property string $email 联系邮箱
 * @property string $brief_desc 项目简介
 * @property string $pc_link 项目web链接
 * @property string $ios_link 项目web链接
 * @property string $android_link 项目app链接
 * @property string $desc 项目完整描述
 * @property string $big_cate
 * @property string $logo_url 应用logo
 * @property string $photo 应用截图
 * @property string $label 项目标签
 * @property int $status 审核状态:0审核中，1审核通过，2不通过
 * @property string $opt_note 审核备注
 * @property int $created_time 项目创建时间
 * @property int $updated_time 项目更新时间
 */
class App extends \yii\db\ActiveRecord
{   
    //审核状态
    const CHECK_STATUS_WATING = 0;//待审核
    const CHECK_STATUS_NOT_PASS = 1;//不通过
    const CHECK_STATUS_PASS = 2;//通过

    // //项目进度
    // const PROGRESS_STATUS_0 = 0;//测试
    // const PROGRESS_STATUS_1 = 1;//发布
    
    public static function getProgressStatus($status=null, $field=null){
         $arr = [
            self::PROGRESS_STATUS_0 => ['id' =>self::PROGRESS_STATUS_0, 'name' => '测试', 'class' =>'label-warning btn-xs'],
            self::PROGRESS_STATUS_1 => ['id' =>self::PROGRESS_STATUS_1,'name' => '发布', 'class' =>'label-success btn-xs'],
        ];
        if (isset($arr[$status][$field])) {
            return $arr[$status][$field];
        }elseif(isset($arr[$status])){
            return Html::tag('span', $arr[$status]['name'], ['class' => 'lable '.$arr[$status]['class']]);
        }

        return array_column($arr, 'name', 'id');
    }

    public static function getCheckStatus($status=null, $field=null){
        $arr = [
            self::CHECK_STATUS_WATING => ['name' => '审核中', 'class' =>'label-warning btn-xs'],
            self::CHECK_STATUS_NOT_PASS => ['name' => '不通过', 'class' =>'label-danger btn-xs'],
            self::CHECK_STATUS_PASS => ['name' => '通过', 'class' =>'label-success btn-xs'],
        ];
        if (isset($arr[$status][$field])) {
            return $arr[$status][$field];
        }elseif(isset($arr[$status])){
            return Html::tag('span', $arr[$status]['name'], ['class' => 'lable '.$arr[$status]['class']]);
        }

        return $arr;
    }

    //获取
    public static function getCheckStatusKeyPair($field='name'){
        $data = self::getCheckStatus();

        $retuen_arr = [];
        foreach ($data as $key => $value) {
            $retuen_arr[$key] = $value[$field];
        }
        return $retuen_arr;
    }

    //使用图片轮播插件
    public function getPictrue($json_picture){
        return '<img src="'.$json_picture.'" width="80px" height="80px">';
    }


    //使用图片轮播插件
    public function getPictrues($json_picture){
        $img_arr = json_decode($json_picture, true);
        $items = [];
        if (is_array($img_arr) && !empty($img_arr)) {
            foreach ($img_arr as $key => $value) {
                //大图
                $items['url'] = $value;
                //原图
                $items['src'] = $value;
                //显示标题
                $items['options'] = ['title' => $key];
            }
        }
        return \dosamigos\gallery\Gallery::widget(['items' => $items]);
    }


    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%app}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uid', 'big_cate', 'status', 'created_time', 'updated_time'], 'integer'],
            [['desc', 'photo', 'opt_note'], 'string'],
            [['name', 'email', 'brief_desc', 'pc_link', 'ios_link', 'android_link', 'logo_url'], 'string', 'max' => 255],
            [['own'], 'string', 'max' => 100],
            [['label'], 'string', 'max' => 50],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'name' => Yii::t('app', '应用名称'),
            'own' => Yii::t('app', '项目开发者'),
            'uid' => Yii::t('app', '上传用户id'),
            'email' => Yii::t('app', '联系邮箱'),
            'brief_desc' => Yii::t('app', '项目简介'),
            'pc_link' => Yii::t('app', '项目web链接'),
            'ios_link' => Yii::t('app', '项目web链接'),
            'android_link' => Yii::t('app', '项目app链接'),
            'desc' => Yii::t('app', '项目完整描述'),
            'big_cate' => Yii::t('app', 'Big Cate'),
            'logo_url' => Yii::t('app', '应用logo'),
            'photo' => Yii::t('app', '应用截图'),
            'label' => Yii::t('app', '项目标签'),
            'status' => Yii::t('app', '审核状态'),
            'opt_note' => Yii::t('app', '审核备注'),
            'created_time' => Yii::t('app', '项目创建时间'),
            'updated_time' => Yii::t('app', '项目更新时间'),
        ];
    }
}
